//jqGrid的配置信息
$.jgrid.defaults.width = 1000;
$.jgrid.defaults.responsive = true;
$.jgrid.defaults.styleUI = 'Bootstrap';

// 工具集合Tools
window.T = {};

// 获取请求参数
// 使用示例
// location.href = http://localhost/index.html?id=123
// T.p('id') --> 123;
var url = function(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null)
		return unescape(r[2]);
	return null;
};
T.p = url;

// 请求前缀（访问静态资源的）
var baseURL = "/huijielong-admin/";
// 请求前缀（向后台请求的，动静分离用）
var apiURL = "/huijielong-api/";

//登录token
var token = localStorage.getItem("token");
if (token == 'null') {
    parent.location.href = baseURL + 'login.html';
}

// jquery全局配置
$.ajaxSetup({
	dataType : "json",
	cache : false,
	headers : {
		"token" : token
	},
	xhrFields : {
		withCredentials : true
	},
	complete : function(xhr) {
		// token过期，则跳转到登录页面
		var tokentmp = localStorage.getItem("token");
		if (tokentmp == 'null' || tokentmp == null) {		
			parent.location.href = baseURL + 'login.html';
			return;
		}
		if (xhr.responseJSON != undefined) {
			if (xhr.responseJSON.code == 401) {			
				parent.location.href = baseURL + 'login.html';
				return;
			}
			var code = xhr.responseJSON.code;
			var msg = xhr.responseJSON.msg;
			if (code != undefined && msg != undefined && code != 0&&code!=2) {
				alert(xhr.responseJSON.msg);
			}
			if(code==2 || code==3){
				confirm(msg+',请重新登陆',function (){
					//删除本地token
                    localStorage.removeItem("token");
                    //跳转到登录页面
                    parent.location.href = baseURL + 'login.html';
				});
			}
			return;
		}
	
	}
});

// jqgrid全局配置
$.extend($.jgrid.defaults, {
	ajaxGridOptions : {
		headers : {
			"token" : token
		}
	}
});

// 权限判断
function hasPermission(permission) {
	if (window.parent.permissions.indexOf(permission) > -1) {
		return true;
	} else {
		return false;
	}
}

// 重写alert

window.alert = function(msg, callback) {
	parent.layer.alert(msg, function(index) {
		parent.layer.close(index);
		if (typeof (callback) === "function") {
			callback("ok");
		}
	});
}

// 重写confirm式样框
window.confirm = function(msg, callback) {
	parent.layer.confirm(msg, {
		btn : [ '确定', '取消' ]
	}, function(index) {// 确定事件
		if (typeof (callback) === "function") {
			callback("ok");
		}
		parent.layer.close(index);
	});
}

// 选择一条记录
function getSelectedRow() {
	var grid = $("#jqGrid");
	var rowKey = grid.getGridParam("selrow");
	if (!rowKey) {
		alert("请选择一条记录");
		return;
	}

	var selectedIDs = grid.getGridParam("selarrrow");
	if (selectedIDs.length > 1) {
		alert("只能选择一条记录");
		return;
	}
	var rowData = grid.getRowData(selectedIDs[0]);
	return selectedIDs[0];
}

// 选择多条记录
function getSelectedRows() {
	var grid = $("#jqGrid");
	var rowKey = grid.getGridParam("selrow");
	if (!rowKey) {
		alert("请选择一条记录");
		return;
	}

	return grid.getGridParam("selarrrow");
}
function getSelectedValue(key) {
	var grid = $("#jqGrid");
	var rowKey = grid.getGridParam("selrow");
	if (!rowKey) {
		alert("请选择一条记录");
		return;
	}

	var selectedIDs = grid.getGridParam("selarrrow");
	if (selectedIDs.length > 1) {
		alert("只能选择一条记录");
		return;
	}
	var rowdata = grid.getRowData(selectedIDs[0]);
	return rowdata[key];
}

function gridBeforeRequest() {
	$("#gridId").jqGrid('setGridParam', {
		postData : {
			enterpriseId : localStorage.getItem("enterpriseId")
		}
	});
}

// 选择多条记录
function getSelectedValues(key) {
	var grid = $("#jqGrid");
	var rowKey = grid.getGridParam("selrow");
	if (!rowKey) {
		alert("请选择一条记录");
		return;
	}
	var selectedIDs = grid.getGridParam("selarrrow");
	var ids = [ selectedIDs.length ];
	for (var i = 0; i < ids.length; i++) {
		var rowdata = grid.getRowData(selectedIDs[0]);
		ids[i] = rowdata[key];
	}
	return ids;
}

// 判断是否为空
function isBlank(value) {
	return !value || !/\S/.test(value)
}
function getQueryParams(query) {
	var params = {};
	for ( var key in query) {
		if (!isBlank(query[key])) {
			params[key] = query[key];
		}
	}
	// 查询前清空参数的缓存
	var postData = $("#jqGrid").jqGrid("getGridParam", "postData");
	$.each(postData, function(k, v) {
		if (!params[k]) {
			delete postData[k];
		}
	});
	return params;
}

var Commons = {
	

	getGridHeight:function(gridId){
		var height=$(document).height();
		var grid=$('#'+gridId);
		if(!grid || !grid.offset()){
			return 500;
		}
		var top = $('#'+gridId).offset().top;
		var gh=height-top-80;
		return gh;
	}
}